package com.sykj.manage.web;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.manage.service.BisNurseBagsService;
import com.sykj.manage.model.BisNurseBag;
import com.sykj.util.MYSConstant;
import com.sykj.util.ManageBody;
import com.sykj.util.PageUtil;

@Controller
@RequestMapping("/bnb")
public class BisNurseBagsController {

	@Autowired
	BisNurseBagsService bisNurseBagsService;
	
	@RequestMapping("/bag")
	@ResponseBody
	public ManageBody getAllBag(BisNurseBag bisNurseBag,Integer offset,Integer pageSize){
		PageHelper.startPage(offset==null?1:PageUtil.getPageNum(offset, pageSize), pageSize==null?10:pageSize, true);
		List<BisNurseBag> getAllBag = bisNurseBagsService.getAllBag(bisNurseBag);
		PageInfo<BisNurseBag> pageInfo = new PageInfo<BisNurseBag>(getAllBag);
		return ManageBody.getData((int)pageInfo.getTotal(), pageInfo.getList(), "");
	}
	
	@RequestMapping("/deleteBag/{bag_id}")
	@ResponseBody
	public ManageBody deleteBag(@PathVariable("bag_id")String bag_id){
		int deleteBag = bisNurseBagsService.deleteBag(bag_id);
		if (deleteBag>0) {
			System.out.println("删除了"+deleteBag+"数据，Id是："+bag_id);
			return ManageBody.getData(1, deleteBag, "");
		} else {
			return ManageBody.getData(1, deleteBag, "");
		}
	}
	
	//添加
	@RequestMapping("/addBag")
	public String addBag(BisNurseBag bisNurseBag,@RequestParam("projectid")List<String> projectid,
			@RequestParam("rmbprice") List<String> rmbprice,@RequestParam("projectcount") List<String> projectcount,
			@RequestParam(value="file",required=false) MultipartFile[] file,HttpServletRequest request)
					throws IOException{
		int count = 1;
		for (MultipartFile mf : file) {
			if (!mf.isEmpty()) {
				//使用UUID给图片重命名，并去掉四个“-”
				String name = UUID.randomUUID().toString().replace("-", "");
				//获取文件的扩展名
				String ext = FilenameUtils.getExtension(mf.getOriginalFilename());
				//设置图片上传路径
				String url = request.getSession().getServletContext().getRealPath("/");
				System.out.println(url);
				//把图片上传路径常量调过来
				String fullPath = MYSConstant.UPLOAD_MANAGE_PROJECT + name + "." + ext;
				File file1 = new File(url+fullPath);
				if (!file1.exists()) {
					file1.createNewFile();
				}
				//以绝对路径保存重命名后的图片
				mf.transferTo(file1);
				//把图片存储路径保存到数据库
				if (count==1) {
					bisNurseBag.setCover_photo(fullPath);
				}else if(count==2){
					bisNurseBag.setCover_big_photo(fullPath);
				}
			}
			count++;
		}
		
		
		bisNurseBag.setCreate_time(new Date());
		int addBag = bisNurseBagsService.addBag(bisNurseBag);
		System.out.println("bag_id ===="+bisNurseBag.getBag_id());
		//插入礼包项目表
				if (projectid!=null) {
					for (int i = 0; i < projectid.size(); i++) {
						System.out.println(projectid.get(i)+"===="+rmbprice.get(i)+"===="+projectcount.get(i));
						Map<String, Object> projectMap = new HashMap<String,Object>();
						projectMap.put("projectid", projectid.get(i));
						projectMap.put("rmbprice", rmbprice.get(i));
						projectMap.put("projectcount", projectcount.get(i));
						projectMap.put("bagid", bisNurseBag.getBag_id());
						bisNurseBagsService.saveBagorPro(projectMap);
					}
				}
		if (addBag>0) {
			System.out.println("=========================================="+bisNurseBag);
		}
		return "redirect:/manage/table_1.html";
	}
	
	//id查询
	@RequestMapping("/findByBagId/{bag_id}")
	@ResponseBody
	public Object Info(@PathVariable("bag_id") String bag_id ){
		//根据id获取所选对象数据
		BisNurseBag findByBagId = bisNurseBagsService.FainByBagAndPro(bag_id);
		System.out.println("==================="+findByBagId);
		return findByBagId;
	}
	
	
	//修改
	@RequestMapping("/updateBag")
	public String updateBag(BisNurseBag bisNurseBag,@RequestParam(value="projectid", required=false)List<String> projectid,
			@RequestParam(value="rmbprice", required=false) List<String> rmbprice,@RequestParam(value="projectcount",required=false) List<String> projectcount,
			@RequestParam(value="file",required=false) MultipartFile[] file,HttpServletRequest request)
					throws IOException{
		int count = 1;
		for (MultipartFile mf : file) {
			if (!mf.isEmpty()) {
				//使用UUID给图片重命名，并去掉四个“-”
				String name = UUID.randomUUID().toString().replace("-", "");
				//获取文件的扩展名
				String ext = FilenameUtils.getExtension(mf.getOriginalFilename());
				//设置图片上传路径
				String url = request.getSession().getServletContext().getRealPath("/");
				System.out.println(url);
				//把图片上传路径常量调过来
				String fullPath = MYSConstant.UPLOAD_MANAGE_PROJECT + name + "." + ext;
				File file1 = new File(url+fullPath);
				if (!file1.exists()) {
					file1.createNewFile();
				}
				//以绝对路径保存重命名后的图片
				mf.transferTo(file1);
				//把图片存储路径保存到数据库
				if (count==1) {
					bisNurseBag.setCover_photo(fullPath);
				}else if(count==2){
					bisNurseBag.setCover_big_photo(fullPath);
				}
			}
			count++;
		}
		
		
		bisNurseBag.setCreate_time(new Date());
		int updateBag = bisNurseBagsService.updateBag(bisNurseBag);
		System.out.println("bag_id ===="+bisNurseBag.getBag_id());
		if (projectid!=null) {
			for (int i = 0; i < projectid.size(); i++) {
				System.out.println(projectid.get(i)+"===="+rmbprice.get(i)+"===="+projectcount.get(i));
				Map<String, Object> projectMap = new HashMap<String,Object>();
				projectMap.put("projectid", projectid.get(i));
				projectMap.put("rmbprice", rmbprice.get(i));
				projectMap.put("projectcount", projectcount.get(i));
				projectMap.put("bagid", bisNurseBag.getBag_id());
				bisNurseBagsService.deleteBagpro(projectMap);
			}
		}
		//插入礼包项目表
				if (projectid!=null) {
					for (int i = 0; i < projectid.size(); i++) {
						System.out.println(projectid.get(i)+"===="+rmbprice.get(i)+"===="+projectcount.get(i));
						Map<String, Object> projectMap = new HashMap<String,Object>();
						projectMap.put("projectid", projectid.get(i));
						projectMap.put("rmbprice", rmbprice.get(i));
						projectMap.put("projectcount", projectcount.get(i));
						projectMap.put("bagid", bisNurseBag.getBag_id());
						bisNurseBagsService.saveBagorPro(projectMap);
					}
				}
		if (updateBag>0) {
			System.out.println("=========================================="+bisNurseBag);
		}
		return "redirect:/manage/table_1.html";
	}
	
	
	/*//修改
		@RequestMapping("/updateBag")
		public String updateType(BisNurseBag bisNurseBag,@RequestParam(value="file",required=false) MultipartFile[] file,HttpServletRequest request)throws IOException{
			int count=1;
		    for (MultipartFile mf : file) {
		        if(!mf.isEmpty()){
		            // 使用UUID给图片重命名，并去掉四个“-”
		            String name = UUID.randomUUID().toString().replaceAll("-", "");
		            // 获取文件的扩展名
		            String ext = FilenameUtils.getExtension(mf
		            .getOriginalFilename());  
		            // 设置图片上传路径
		            String url = request.getSession().getServletContext().getRealPath("/");
		            System.out.println(url);
		            //把图片上传路径的常量调用过来
		            String fullPath =  MYSConstant.UPLOAD_MANAGE_PROJECT + name + "." + ext;
		            File file1 = new File(url + fullPath);
		            if (!file1.exists()) {
		            	file1.createNewFile();
		            }
		            // 以绝对路径保存重名命后的图片
		            mf.transferTo(file1);
		            // 把图片存储路径保存到数据库
		            if(count==1){
		            	bisNurseBag.setCover_photo(fullPath);
		            }else if(count==2){
		            	bisNurseBag.setCover_big_photo(fullPath);
		            }
		        }  
		        count++;
		    }
			
		   int updateBag = bisNurseBagsService.updateBag(bisNurseBag);
		   if (updateBag>0) {
				System.out.println("====================修改了"+updateBag);
		   }
		   return "redirect:/manage/table_1.html";
		}*/
}
